package cc.bukkitPlugin.banitem.listener;

import java.util.ArrayList;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.util.Vector;

import cc.bukkitPlugin.banitem.BanItem;
import cc.bukkitPlugin.banitem.BanItemEnume.ListType;
import cc.bukkitPlugin.banitem.check.ItemCheck;
import cc.bukkitPlugin.banitem.manager.PermissionMan;
import cc.bukkitPlugin.util.plugin.ABukkitPlugin;

public class BlockListener extends APreventListener{

    //private boolean mCheckBlockAroundWhenPlace=false;
    private static ArrayList<Vector> mAroundLoc=new ArrayList<>();

    static{
        mAroundLoc.add(new Vector(0,0,1));
        mAroundLoc.add(new Vector(0,0,-1));
        mAroundLoc.add(new Vector(0,1,0));
        mAroundLoc.add(new Vector(0,-1,0));
        mAroundLoc.add(new Vector(1,0,0));
        mAroundLoc.add(new Vector(-1,0,0));
    }

    /**
     * 实例化一个监听器,会自己注册到监听列表
     */
    public BlockListener(BanItem pPlugin){
        super(pPlugin);
    }

    @EventHandler(priority=EventPriority.HIGHEST)
    public void onBlockBreak(BlockBreakEvent pEvent){
        Player tPlayer=pEvent.getPlayer();
        if(tPlayer.hasPermission("BanItem.bypass"))
            return;
        Block breakBlock=pEvent.getBlock();
        if((tCheckResult=this.getItemChecker().checkExist(breakBlock,tPlayer.getWorld().getName(),ListType.BAN)).mFind){
            if(this.getPermissionMan().hasPermission(tPlayer,breakBlock))
                return;
            pEvent.setCancelled(true);
            ABukkitPlugin.send(tPlayer,tCheckResult.mReason);
            return;
        }
    }

    @EventHandler(priority=EventPriority.HIGHEST)
    private void onPlayerPlaceBlock(BlockPlaceEvent pEvent){
        Player tPlayer=pEvent.getPlayer();
        if(tPlayer.hasPermission("BanItem.bypass"))
            return;
        Block placedBolck=pEvent.getBlockPlaced();
        if(placedBolck==null)
            return;
        ItemCheck tItemCheck=this.getItemChecker();
        PermissionMan tPermissionMan=this.getPermissionMan();
        // 检查放置的方块是否在黑名单
        if((tCheckResult=tItemCheck.checkExist(placedBolck,tPlayer.getWorld().getName(),ListType.BAN)).mFind){
            if(tPermissionMan.hasPermission(tPlayer,placedBolck))
                return;
            pEvent.setCancelled(true);
            ABukkitPlugin.send(tPlayer,tCheckResult.mReason);
            return;
        }
        if(this.mPlugin.getConfigManager().isCheckBlockAroundWhenPlace()){ //检查方块周围
            World tWorld=placedBolck.getWorld();
            for(Vector checkAdd : BlockListener.mAroundLoc){
                Location checkLoc=placedBolck.getLocation().add(checkAdd);
                Block aroundBlock=tWorld.getBlockAt(checkLoc);
                if(aroundBlock==null||aroundBlock.getType()==Material.AIR)
                    continue;
                if((tCheckResult=tItemCheck.checkExist(aroundBlock,tPlayer.getWorld().getName(),ListType.BAN)).mFind){
                    if(tPermissionMan.hasPermission(tPlayer,placedBolck))
                        continue;
                    pEvent.setCancelled(true);
                    ABukkitPlugin.send(tPlayer,"你不能放置这个方块,因为周围的方块包含黑名单物品");
                    return;
                }
            }
        }
    }

}
